package mlp.game.cherries;

import com.overkill.game.Sprite;
import com.overkill.game.SpriteRenderable;

import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.RectF;

public class Cherry extends SpriteRenderable {
	
	private int color = Color.RED;
	private boolean canBeDragged = true;
	private boolean dragged = false;
	private float angle;
	private float scale = 1;
	
	public Cherry(Sprite sprite, int color) {
		super(sprite);
		this.color = color;
		angle = (float) ((Math.random() * (360 * 2)) - 360);
		setCanBeDragged(true);
	}

	public void dropped(){
		scale = 0.99f;
	}
	
	@Override
	public void update(long gameTime) {
		if(isDragged() == false){
			moveXBy(CherryEngine.GAME_SPEED);
			if(scale >= 1f){
				scale = 1f;
			}else{
				angle += 5;
				scale -= 0.06f;
			}
		} else {
			scale = 1.2f;
		}
		if(scale <= 0){
			setVisible(false);
		}
		super.update(gameTime);
	}
	
	@Override
	public RectF getFrame() {
		return new RectF(getPosition().x - (getWidth() / 2),
				getPosition().y,
				getPosition().x + (getWidth() / 2) + getWidth(),
				getPosition().y + getHeight());
	}
	
	@Override
	public int getWidth() {
		return (int) (super.getWidth() * scale);
	}
	
	@Override
	public int getHeight() {
		return (int) (super.getHeight() * scale);
	}
	
	@Override
	public void draw(Canvas canvas) {
//		if(CherryEngine.SHOW_DEBUG_INFO){
//	    	canvas.drawRect(getFrame(), Sprite.debugPaint);
//    	}
		Matrix matrix = canvas.getMatrix();
		canvas.rotate(angle, getFrame().centerX(), getFrame().centerY());
		canvas.scale(scale, scale, getFrame().centerX(), getFrame().centerY());
		getSprite().drawAt(getPosition(), canvas);
		canvas.setMatrix(matrix);
	}
		
	public int getColor() {
		return color;
	}

	public void setColor(int color) {
		this.color = color;
	}

	public boolean isDragged() {
		return dragged;
	}

	public void setDragged(boolean dragged) {
		this.dragged = dragged;
	}

	public void setCanBeDragged(boolean canBeDragged) {
		this.canBeDragged = canBeDragged;
	}

	public boolean canBeDragged() {
		return canBeDragged;
	}
}
